package vcp.gui.forms;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import vcp.common.IView;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.validators.NumericValidator;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import java.awt.Color;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JRadioButton;

import client.Message;
import javax.swing.border.TitledBorder;



public class OrderSubscriptionForm extends VCPPanel implements Observer,IView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textCID;
	private JLabel lblNewOrderError;

	private JRadioButton regularSRadioButton;
	private JRadioButton fullButton;
 	
	/* validators */
	private boolean isValidForm=false;
	private JTextField carNo_field;
	private JButton btnSend;
	
	public OrderSubscriptionForm() {
		setBorder(new TitledBorder(null, "Subscription order", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(Color.WHITE);
		  buildForm();		
	}
	
	private void buildForm(){

		textCID = new JTextField();
		textCID.setColumns(10);
		JLabel lblSID = new JLabel("Subscriber ID");
		btnSend = new JButton("create");

		lblNewOrderError = new JLabel("");
		lblNewOrderError.setForeground(Color.RED);
		carNo_field = new JTextField();
		carNo_field.setColumns(10);
		JLabel lblCarNumber = new JLabel("Car Number");
		regularSRadioButton = new JRadioButton("Regular");
		fullButton = new JRadioButton("Full");
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblSID)
								.addContainerGap(365, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblCarNumber, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(365, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addGap(85)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(textCID, Alignment.TRAILING)
											.addComponent(carNo_field, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
											.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
												.addGap(44)
												.addComponent(regularSRadioButton)
												.addPreferredGap(ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
												.addComponent(fullButton, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
												.addGap(65))))
									.addComponent(lblNewOrderError))
								.addGap(16)))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnSend)
							.addGap(181))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(lblNewOrderError)
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSID)
						.addComponent(textCID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCarNumber)
						.addComponent(carNo_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(fullButton)
						.addComponent(regularSRadioButton))
					.addGap(44)
					.addComponent(btnSend)
					.addContainerGap(72, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}

	@Override
	public void addController(Object obj) {
		btnSend.addActionListener((ActionListener)obj);
		btnSend.setActionCommand("orderSubscription");
	}

	@Override
	public void update(Observable o, Object arg) {
		Message tempMsg = (Message)arg;  
		switch(tempMsg.getMsg())
		 {
		 	case "AddSubscriberOrder":
		 		
				if(tempMsg.isStatus())
				{
					JOptionPane.showMessageDialog(null , "Order is Succesful");
				}	
				else
				{
					JOptionPane.showMessageDialog(null,"Order has Failed: " + tempMsg.getStatusMsg(),"DATABASE ERROR",JOptionPane.ERROR_MESSAGE);
				}
				break;
		 }
}

	public JTextField getTextCID() {
		return textCID;
	}

	public void setTextCID(JTextField textCID) {
		this.textCID = textCID;
	}

	public JLabel getLblNewOrderError() {
		return lblNewOrderError;
	}

	public void setLblNewOrderError(JLabel lblNewOrderError) {
		this.lblNewOrderError = lblNewOrderError;
	}

	public JRadioButton getRegularSRadioButton() {
		return regularSRadioButton;
	}

	public void setRegularSRadioButton(JRadioButton regularSRadioButton) {
		this.regularSRadioButton = regularSRadioButton;
	}

	public JRadioButton getFullButton() {
		return fullButton;
	}

	public void setFullButton(JRadioButton fullButton) {
		this.fullButton = fullButton;
	}

	public JTextField getCarNo_field() {
		return carNo_field;
	}

	public void setCarNo_field(JTextField carNo_field) {
		this.carNo_field = carNo_field;
	}

	public JButton getBtnSend() {
		return btnSend;
	}

	public void setBtnSend(JButton btnSend) {
		this.btnSend = btnSend;
	}

}


